package com.example.xiaozu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.xiaozu.R;
import com.example.xiaozu.model.Product;
import com.example.xiaozu.util.CartManager;

public class ProductDetailActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);

        Product product = (Product) getIntent().getSerializableExtra("product");

        ImageView ivProduct = findViewById(R.id.iv_detail_product);
        TextView tvName = findViewById(R.id.tv_detail_name);
        TextView tvDescription = findViewById(R.id.tv_detail_description);
        TextView tvPrice = findViewById(R.id.tv_detail_price);
        Button btnAddToCart = findViewById(R.id.btn_add_to_cart);
        Button btnBuyNow = findViewById(R.id.btn_buy_now);

        ivProduct.setImageResource(product.getImageResId());
        tvName.setText(product.getName());
        tvDescription.setText(product.getDescription());
        tvPrice.setText("¥" + product.getPrice());

        btnAddToCart.setOnClickListener(v -> {
            CartManager.getInstance().addToCart(product, 1);
            Toast.makeText(this, "已添加到购物车", Toast.LENGTH_SHORT).show();
        });

        btnBuyNow.setOnClickListener(v -> {
            // 立即购买逻辑
        });

        // 在onCreate方法中找到购物车图标并添加点击事件
        findViewById(R.id.iv_cart).setOnClickListener(v -> {
            startActivity(new Intent(ProductDetailActivity.this, CartActivity.class));
        });
    }
}